#include <iostream>
#include<unistd.h>
#include<string.h>
#include <sys/types.h>
#include <sys/wait.h>
//usiang namespace std;
int main()
{
  pid_t id =fork();
  if(id < 0)
  {
    printf("errno : %d, errstring: %s\n",errno,strerror(errno));
  }
  else if(id == 0)
  {
    int cnt = 5;
    while(cnt)
    {
      printf("子进程运行中,pid: %d\n",getpid());
      cnt--;
      sleep(1);
    }
    exit(0);
  }
  else
  {
    sleep(10);
    pid_t rid = waitpid(id,nullptr,0);                                                                                         
        if(rid > 0)
        {
          printf("子进程回收成功, rid: %d\n",rid);
        }
    while(true)
    {
      printf("我是父进程,pid: %d\n",getpid());
      sleep(1);
    }
  }
  return 0;
}
